package com.central.user.util;

import com.central.model.common.Result;
import com.jcraft.jsch.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

public class SSHUtil {
    private static long INTERVAL = 100L;
    private static int SESSION_TIMEOUT = 20000;
    private static int CHANNEL_TIMEOUT = 3000;
    private static int SESSION_CLOSE = 30000;
    private JSch jsch = null;
    private Session session = null;
    private Timer timer;
    private TimerTask timerTask;

    private SSHUtil(SSHInfo sshInfo) throws JSchException {
        jsch = new JSch();
        session = jsch.getSession(sshInfo.getUser(), sshInfo.getHost(), sshInfo.getPort());
        session.setPassword(sshInfo.getPassword());
        UserInfo userInfo = new UserInfo() {
            @Override
            public String getPassphrase() {
                System.out.println("getPassphrase");
                return null;
            }

            @Override
            public String getPassword() {
                System.out.println("getPassword");
                return null;
            }

            @Override
            public boolean promptPassword(String s) {
                System.out.println("promptPassword:" + s);
                return false;
            }

            @Override
            public boolean promptPassphrase(String s) {
                System.out.println("promptPassphrase:" + s);
                return false;
            }

            @Override
            public boolean promptYesNo(String s) {
                System.out.println("promptYesNo:" + s);
                return true;//notice here!
            }

            @Override
            public void showMessage(String s) {
                System.out.println("showMessage:" + s);
            }
        };
        session.setUserInfo(userInfo);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect(SESSION_TIMEOUT);
    }

    public static SSHUtil newInstance(String user, String password, String host) throws JSchException {
        SSHInfo sshInfo = new SSHUtil.SSHInfo(user, password, host, 22);
        SSHUtil sshUtil = new SSHUtil(sshInfo);
        sshUtil.timer = new Timer();
        sshUtil.startTimer();
        return sshUtil;
    }

    public TimerTask timerCloseSession(){
        timerTask = new TimerTask() {
            @Override
            public void run() {
                close();
            }
        };
        return timerTask;
    }

    public void startTimer(){
        timer.schedule(timerCloseSession(),SESSION_CLOSE);
    }

    public Result exec(String cmd) throws IOException, JSchException, InterruptedException {
        timer.cancel();
        timer = new Timer();
        startTimer();
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(cmd);
        channel.connect(CHANNEL_TIMEOUT);
        InputStream input = channel.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[2048];
        int count = input.read(buf);
        while (count > 0) {
            bos.write(buf, 0, count);
            count = input.read(buf);
        }
        byte[] temp = bos.toByteArray();
        String msg = new String(temp);
        System.out.println(msg);
        bos.close();
        input.close();
        channel.disconnect();
        return Result.succeed(0,msg);
    }

    public  Session getSession() {
        return session;
    }

    public Result close() {
        getSession().disconnect();
        return Result.succeed(0,"关闭连接");
    }

    public static class SSHInfo {
        private String user;
        private String password;
        private String host;
        private int port;

        public SSHInfo(String user, String password, String host, int port) {
            this.user = user;
            this.password = password;
            this.host = host;
            this.port = port;
        }

        public String getUser() {
            return user;
        }

        public String getPassword() {
            return password;
        }

        public String getHost() {
            return host;
        }

        public int getPort() {
            return port;
        }
    }

    public static void main(String[] args) throws Exception {
        SSHUtil sshUtil = SSHUtil.newInstance("root","BsdldSsh123456","47.111.83.79");
        System.out.println("ssss");
    }

}